clear all
*cap log close
set more off
set seed 603


* plot summarizing output of FLL (2023) test ------------------------------------------

* get info from subset test -------------
use "${est}/fll_sub", clear
summ Fstat 
local f = r(mean)
summ pval
local p = r(mean)
local f2 = "F = `:di %4.3f `f'' (p = `:di %4.3f `p'')"
if `p'<0.001 {
	local f2 = "F = `:di %4.3f `f'' (p < 0.001)"
}

* store judges --------------
drop if mi(jid)
keep jid
duplicates drop
tempfile list
save    `list'

* setup for plot with full sample -------------------
* store statistics ----
use "${est}/fll_full", clear
summ Fstat 
local f = r(mean)
summ pval
local p = r(mean)
local f1 = "F = `:di %4.3f `f'' (p = `:di %4.3f `p'')"
if `p'<0.001 {
	local f1 = "F = `:di %4.3f `f'' (p < 0.001)"
}

* store fitted line -------
use "${est}/fll_full", clear
* bounds for fitted line 
bysort jid: egen muZ = mean(rZ)
summ muZ, d
local min = r(min)
local max = r(max)
keep if !mi(x)
keep if x>=`min'& x<=`max'
* store output 
keep x y 
ren x rZ 
ren y fitY 
tempfile fit
save    `fit'

* create officer-level scatter -----------
use "${est}/fll_full", clear
drop if mi(jid)
gen N=1
collapse (sum) N (mean) rZ rY, by(jid)

merge 1:1 jid using `list', keep(1 3)
gen jdrop = (_m==1)
gen jkeep = (_m==3)
drop _m 
merge 1:1 rZ using `fit', nogen

* build plot ---------------------------------------
#delimit ;
twoway
scatter rY rZ [aw=N] if jkeep==1, msymbol(Oh) mcolor(emidblue) msize(tiny) ||
scatter rY rZ [aw=N] if jdrop==1, msymbol(Sh) mcolor(maroon) msize(tiny) ||
line fitY rZ, lwidth(thick) lcolor(forest_green)
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin))
ylab(-0.2(0.1)0.2,nogrid) xlab(-0.7(0.35)0.7,nogrid) legend(off)
xtitle("Officer Stringency") ytitle("Pr(Reoffend)") 
text(-0.132 0.65 "Full Sample:" "`f1'", place(w) ) 
text(-0.173 0.65 "Drop 12% of Officers:" "`f2'", place(w)) ;
#delimit cr
graph export "${out}/apx_iv/fll_test.pdf", replace 
* -------------------------------------------------------------------------------------



* tests of avg monotonicity -----------------------------------------------------------
* (first stage by subgroup)

* setup data: binary categories w latex lables -------
use "${data}/out/4-main", clear

gen old = age>30 
label var old "Age $>$ 30"
gen race_w = (race=="W")
label var race_w "Race $=$ White"
label var race_b "Race $=$ Black"
label var race_h "Race $=$ Hispanic"
label var race_o "Race $=$ Other"
label var race_u "Race $=$ Unknown"
gen rich = zipincome > 50 
label var rich "Zip Income $>$ \\\\$50,000"
gen price = exp(logprice)
gen fancy = price > 20000
label var fancy "Vehicle $>$ \\\\$20,000"
label var cite_py1 "Citation Past Year"
local list = "female old race_w race_b race_h race_o race_u local rich fancy cite_py1"

* create variables for storage -------
gen split = "" 
gen b1    = . 
gen s1    = .
gen p1    = .
gen b0    = . 
gen s0    = . 
gen p0    = . 

* estimate regressions by group ----------
local k = 1
foreach VAR in `list' {

	local lab: variable label `VAR' 
	replace split = "`lab'" if _n == `k'

	reghdfe harsh Z if `VAR' == 1, absorb(totfe) vce(cluster officerid)
	replace b1 =  _b[Z] if _n == `k'
	replace s1 = _se[Z] if _n == `k'
	test Z
	replace p1 = r(p) if _n == `k'

	reghdfe harsh Z if `VAR' == 0, absorb(totfe) vce(cluster officerid)
	replace b0 =  _b[Z] if _n == `k'
	replace s0 = _se[Z] if _n == `k'
	test Z 
	replace p0 = r(p) if _n == `k'

	local ++k
}

* format estimates for latex table ------
foreach i in 1 0 {
	gen bwrite_`i' = string(b`i',"%4.3f")
	gen swrite_`i' = string(s`i', "%4.3f")
	replace swrite_`i' = "(" + swrite_`i' + ")"	
}


* post output to LaTeX table ---------------------
capture erase "${out}/apx_iv/table_mono.tex"
file open fh using "${out}/apx_iv/table_mono.tex", write replace
file write fh ///	
	"&& \multicolumn{2}{c}{Subgroup} \\  \cline{3-4}" _n /// 
	"&& \multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)} \\" _n ///
	"&& \multicolumn{1}{c}{$=1$}&\multicolumn{1}{c}{$=0$} \\" _n ///
	"\hline" _n
	
count if !mi(split)
local nrow = r(N)
forval i = 1/`nrow' {
	
	local label = split[`i']
	local m1    = bwrite_1[`i']
	local m2    = bwrite_0[`i']	
	file write fh "`label' && `m1' & `m2' \\" _n

	local m1    = swrite_1[`i']
	local m2    = swrite_0[`i']
	file write fh "        && `m1' & `m2' \\" _n

	if `i' < `nrow' {
		file write fh "[1em]" _n 
	}
}

file write fh "\bottomrule" _n  
file close fh
macro drop fh
* -------------------------------------------------------------------------------------






